import axios from 'axios'
// import JSONbig from 'json-bigint'
import router from '../router'
import store from '../store'


const request = axios.create({
    timeout: 600000,
})
let baseURL = 'http://39.100.39.50:5051'
// baseURL = 'http://touzi.cjwltzj.taiyuan.gov.cn'
let baseURL1 = 'http://touzi.cjwltzj.taiyuan.gov.cn'
let baseURL2 = 'https://zysf-store.oss-cn-beijing.aliyuncs.com'
let baseURL3 = 'http://192.168.1.13:5000'
let baseURL4 = 'http://39.100.39.50:8006'
// let baseURL5 = 'http://localhost:8080'
let baseURL5 = 'http://43.138.44.242/prod-api'
/*
 处理后端返回数据中数字超出 JavaScript 安全整数范围
 */
// request.defaults.transformResponse = [function (data) {
//     try {
//         return JSONbig.parse(data)
//         // 如果是 json 格式字符串，那就转换并返回给后续使用
//     } catch (err) {
//         // 报错就意味着 data 不是 json 格式字符串，这里就直接原样返回给后续使用
//         return data
//     }
// }]

// Add a request interceptor
request.interceptors.request.use(function (config) {
    // dataType: 'jsonp',
    // axios.defaults.withCredentials = true;
    // let login =
    switch (config.urlType) {
        case 'api1':
            config.url = baseURL1 + config.url;
            break;
        case 'api2':
            config.url = baseURL2 + config.url;
            break;
        case 'api3':
            config.url = baseURL3 + config.url;
            break;
        case 'api4':
            config.url = baseURL4 + config.url;
            break;
        case 'api5':
            config.url = baseURL5 + config.url;
            break;
        default:
            config.url = baseURL + config.url;
    }
    // console.log(config);
    config.headers = {
        token: localStorage.getItem('login')
    }
    if (config.url == "/api/user/login_bypwd") {
        return config;
    }
    else if (config.url == `${baseURL5}/common/upload`) {
        config.headers = {
            token: localStorage.getItem('login'),
            'Content-Type': 'multipart/form-data'
        }
        return config;
    }
    // else if (config.url === `${baseURL5}/push/push/api/projectByFenfa`) {
    //     config.headers.token = localStorage.getItem('login')
    //     config.headers['Content-Type'] = 'application/json;charset=UTF-8'
    //     return config
    // } 
    else {
        // 在此设置请求头统一携带token
        config.headers.token = localStorage.getItem('login')
        return config;
    }
}, function (error) {
    // Do something with request error
    return Promise.reject(error)
})

// Add a response interceptor
request.interceptors.response.use(function (response) {
    if (response.data.code == '4101') {
        const url = window.location.href;
        localStorage.setItem("path", url);
        localStorage.removeItem("token")
        store.commit("setRoleId", 0);
        store.commit("setFlag", 0)
        setTimeout(function () {
            router.replace({
                path: '/'
            });
        }, 1000)
    }
    return response.data.data || response.data || response
    // return response
}, function (error) {
    // Do something with response error
    return Promise.reject(error)
})

export default request
